using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace SecurityLinkUp
{
    public partial class ChapterCommunityRequest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // populate the parent community dropdown
            ArrayList communities = CommunityData.GetCommunities(CommonData.Status.ACTIVE);
            foreach (Community c in communities)
            {
                ListItem itm = new ListItem(c.name, c.uid.ToString());
                ParentCommunity.Items.Add(itm);
            }
        }

        protected void SaveCommunityBtn_Click(object sender, EventArgs e)
        {
            // do some basic checks
            if (CommunityName.Text.Trim() == "")
            {
                CommunityErrorMessage.Text = "Community name cannot be blank";
                return;
            }

            if (CommunityDescription.Text.Trim() == "")
            {
                CommunityErrorMessage.Text = "A community description should be provided";
                return;
            }

            // create a new community
            Community c = new Community(Guid.Empty, CommunityName.Text, CommunityDescription.Text, CommonData.Status.REQUEST, CommunityReason.Text);

            // save the community to the database
            string result = CommunityData.SaveCommunityToDB(c);
            if (result == "")
            {
                CommunityErrorMessage.Text = "Successfully saved. The chapter will be displayed once administrator has approved it";
            }
            else
            {
                CommunityErrorMessage.Text = result;
            }
        }

        protected void SaveChapterBtn_Click(object sender, EventArgs e)
        {
            // do some basic checks
            if (ChapterName.Text.Trim() == "")
            {
                ChapterErrorMessage.Text = "Chapter name cannot be blank";
                return;
            }

            if (ChapterDescritpion.Text.Trim() == "")
            {
                ChapterErrorMessage.Text = "A chapter description should be provided";
                return;
            }

            if (ParentCommunity.Text == "")
            {
                ChapterErrorMessage.Text = "A parent community must be provided";
                return;
            }

            // create a new chapter
            Chapter c = new Chapter(Guid.Empty, new Guid(ParentCommunity.SelectedValue), ChapterName.Text, ChapterDescritpion.Text, CommonData.Status.REQUEST, ChapterReason.Text);

            // if there's a geo location, add the details to the chapter
            if ((GeoLat.Value != "") && (GeoLong.Value != ""))
            {
                c.SetGeoData(ChapterGeoArea.Text, double.Parse(GeoLat.Value), double.Parse(GeoLong.Value));
            }

            // save the chapter to the database
            string result = ChapterData.SaveChapterToDB(c);
            if (result == "")
            {
                ChapterErrorMessage.Text = "Successfully saved. The chapter will be displayed once administrator has approved it";
            }
            else
            {
                ChapterErrorMessage.Text = result;
            }
        }

        
    }
}
